package com.cosina.mr.util;

import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

import com.cosina.mr.R;

public class SimpleDatePicker extends EditText {
	private final String format;
	public SimpleDatePicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DatePattern);
		format = a.getString(R.styleable.DatePattern_pattern);
		
		Log.e("cosina1985", "format:" + format);
		
		this.setOnClickListener(cl);
		this.setText(DateUtil.format(new Date(),format));
	}
	
	@Override
    protected boolean getDefaultEditable() {
        return false;
    }
	
	private OnClickListener cl = new OnClickListener(){

		@Override
		public void onClick(View v) {
			Calendar calendar = Calendar.getInstance();
			DatePickerDialog dialog = new DatePickerDialog(SimpleDatePicker.this.getContext(),dsl,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
			dialog.show();
		}
		
	};
	private OnDateSetListener dsl = new OnDateSetListener(){

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			Calendar calendar = Calendar.getInstance();
			calendar.set(year, monthOfYear, dayOfMonth);
			SimpleDatePicker.this.setText(DateUtil.format(calendar.getTime(),format));
		}
	};
}
